# -*- coding: utf-8 -*-
# @Time    : 2021/11/17 16:10
# @Author  : CHENlj
# @File    : shake.py
# @Software: PyCharm
"""
摇一摇概率验证
"""
import requests

token = '202824e3ed8b94c7387efd11fd4b51e7'
host = 'http://8.134.36.205:8201'
path = '/redPacket/shake'
url = host + path

headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Token": token
}
count = 0  # 匹配失败统计
num = 100  # 摇的次数
shake_num1 = 0  # 从第几次开始摇
shake_num2 = 0  # 结束时候今天累计摇的次数
for i in range(0, num):
    res = requests.post(url, headers=headers)
    print('=====第'+str(i+1)+'次摇=====')
    try:
        if i == 0:
            shake_num1 = res.json()['DATA']['shake_num']
        elif i == num-1:
            shake_num2 = res.json()['DATA']['shake_num']
        if res.json()['DATA']['shake_user'] is None:
            count += 1
            print('没有匹配到机器人！！！')
        if i == 0:
            file_handle = open('shake_data.txt', mode='w', encoding='gbk')
        else:
            file_handle = open('shake_data.txt', mode='a', encoding='gbk')
        file_handle.write(res.json()+'\n')
        file_handle.close()
    except Exception:
        if i == 0:
            file_handle = open('shake_data.txt', mode='w', encoding='gbk')
        else:
            file_handle = open('shake_data.txt', mode='a', encoding='gbk')
        file_handle.write(res.text + '\n')
        file_handle.close()
rate = (num - count)/num * 100
print('\n今天从第%d次开始摇，摇到第%d次,一共摇了%d次' % (shake_num1, shake_num2, shake_num2-shake_num1+1))
print('共有%d次没有匹配到机器人' % count)
print('匹配成功率为：%f' % rate+' %')



